package com.openflag.bwicservice.controller;


import com.openflag.bwicservice.domain.Bwic;
import com.openflag.bwicservice.domain.CancelRequest;
import com.openflag.bwicservice.domain.Result;
import com.openflag.bwicservice.domain.WarrantAgreeRequest;
import com.openflag.bwicservice.repository.BwicRepository;
import com.openflag.bwicservice.service.SellerService;
import com.openflag.bwicservice.service.impl.SellerServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.openflag.bwicservice.domain.ResultStatus.*;
import static com.openflag.bwicservice.util.ResultUtil.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/seller")
@CrossOrigin(origins = "*")
public class SellerController {

    @Autowired
    SellerService sellerService;






    @PostMapping("/warrantagreement")
    public Result<List<Bwic>> warrantagreement(@RequestBody WarrantAgreeRequest warrantAgreeRequest){
        String status = sellerService.warrantAgreementCheck(warrantAgreeRequest);

        Result<List<Bwic>> result = new Result<List<Bwic>>();

        switch (status){
            case "success":
                sellerService.sellerAddBwic(warrantAgreeRequest);
                List<Bwic> bwicList = new ArrayList<>();
                bwicList = sellerService.sellerBwicList(warrantAgreeRequest.getCreatedById());
                result = success(bwicList);
                break;

            case "Cusip duplication":
                result = fail(WARRANT_CUSIP_DUPLICATION);
                break;

            case "DueDate is before createdDate":
                result = fail(WARRANT_DUDATE_WRONG);
                break;

            case "Position is not positive":
                result = fail(WARRANT_POSITION_WRONG);
                break;

            default:
                result = fail(404,"Not found");
                break;
        }
        return result;

    }

    @GetMapping("/warrantList/{sellerId}")
    public Result<List<Bwic>> sellerBwicList(@PathVariable Long sellerId){
        List<Bwic> bwicList = new ArrayList<>();
        bwicList = sellerService.sellerBwicList(sellerId);
        return success(bwicList);

    }

    @PostMapping("/cancel")
    public Result<List<Bwic>> sellerCancelBwic(@RequestBody CancelRequest cancelRequest){
        List<Bwic> bwicList = new ArrayList<>();
        Result result = sellerService.sellerCancelBwic(cancelRequest);

        return result;

    }

    

}
